<!-- 添加歌曲到列表层 -->
<template>
  <transition name="slide">
    <div class="add-song"  v-show="showFlag" @click.stop>
      <!-- 头部 -->
      <div class="header">
        <h1 class="title">添加歌曲到列表</h1>
        <!-- 点击关闭本列表 -->
        <div class="close" @click="hide">
          <i class="icon-close"></i>
        </div>
      </div>
      <!-- 搜索框 -->
      <div class="search-box-wrapper">
        <search-box ref="searchBox" @query="onQueryChange" placeholder="搜索歌曲"></search-box>
      </div>
      <!-- 歌曲列表 -->
      <!-- 显示隐藏依靠searchbox的输入值有无 -->
      <div class="shortcut" v-show="!query">
        <!-- 组件是添加歌曲列表中切换tab -->
        <switches @switch="switchItem" :switches="switches" :currentIndex="currentIndex"></switches>
        <!-- 滚动列表 -->
        <div class="list-wrapper">
          <scroll ref="songList" class="list-scroll" v-if="currentIndex === 0" :data="playHistory">
            <div class="list-inner">
              <!-- 歌曲列表基础组件 -->
              <song-list :songs='playHistory' @select="selectSong"></song-list>
            </div>
          </scroll>
          <scroll ref="searchList" :refreshDelay="refreshDelay" class="list-scroll" v-if="currentIndex === 1" :data="searchHistory">
            <div class="list-inner">
              <search-list @delete="deleteSearchHistory" @select="addQuery" :searches="searchHistory"></search-list>
            </div>
          </scroll>
        </div>
      </div>
      <div class="search-result" v-show="query">
        <suggest :showSinger="showSinger" :query="query" @select="selectSuggest" @listScroll="blurInput"></suggest>
      </div>
      <!-- 顶部提示组件 -->
      <top-tip ref="topTip">
        <div class="tip-title">
          <i class="icon-ok"></i>
          <span class="text">1首歌曲已经添加到播放列表</span>
        </div>
      </top-tip>
    </div>
  </transition>
</template>

<script>
// 搜索页面的搜索框
import SearchBox from 'base/search-box/search-box'
// 搜索之后的返回的选项组件
import Suggest from 'components/suggest/suggest'
// 引入mixin方法
import {searchMixin} from 'common/js/mixin'
// 引入tab组件
import Switches from 'base/switches/switches'
// scroll组件
import Scroll from 'base/scroll/scroll'
// vuex
import {mapGetters, mapActions} from 'vuex'
// song-list歌曲列表基础组件
import SongList from 'base/song-list/song-list'
// 引进这个song类
import Song from 'common/js/song'
// 搜索列表组件
import SearchList from 'base/search-list/search-list'
// 顶部提示组件
import TopTip from 'base/top-tip/top-tip'

export default {
  mixins: [searchMixin],
  data () {
    return {
      showFlag: false,
      showSinger: false,
      // 给switches组件的索引
      currentIndex: 0,
      // 给switches组件的数据
      switches: [
        {name: '最近播放'},
        {name: '搜索历史'}
      ]
    }
  },
  computed: {
    ...mapGetters([
      'playHistory'
    ])
  },
  methods: {
    show () {
      this.showFlag = true
      setTimeout(() => {
        if (this.currentIndex === 0) {
          // 刷新scroll高度
          this.$refs.songList.refresh()
        } else {
          this.$refs.searchList.refresh()
        }
      })
    },
    hide () {
      this.showFlag = false
    },
    // 点击suggest页面的回调函数
    selectSuggest () {
      // 从mixin中来的
      this.saveSearch()
      // 顶部提示
      this.showTip()
    },
    // 监听switches传递来的点击派发事件
    switchItem (index) {
      // 修改传递给switches组件的currentIndex
      this.currentIndex = index
    },
    // 监听子组件传递来的数据
    selectSong (song, index) {
      if (index !== 0) {
        this.insertSong(new Song(song))
        // 顶部提示
        this.showTip()
      }
    },
    // 调用子组件的方法
    showTip () {
      this.$refs.topTip.show()
    },
    ...mapActions([
      'insertSong'
    ])
  },
  components: {
    SearchBox,
    Suggest,
    Switches,
    Scroll,
    SongList,
    SearchList,
    TopTip
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .add-song
    position: fixed
    top: 0
    bottom: 0
    width: 100%
    z-index: 200
    background: $color-background
    &.slide-enter-active, &.slide-leave-active
      transition: all 0.3s
    &.slide-enter, &.slide-leave-to
      transform: translate3d(100%, 0, 0)
    .header
      position: relative
      height: 44px
      text-align: center
      .title
        line-height: 44px
        font-size: $font-size-large
        color: $color-text
      .close
        position: absolute
        top: 0
        right: 8px
        .icon-close
          display: block
          padding: 12px
          font-size: 20px
          color: $color-theme

    .search-box-wrapper
      margin: 20px
    .shortcut
      .list-wrapper
        position: absolute
        top: 165px
        bottom: 0
        width: 100%
        .list-scroll
          height: 100%
          overflow: hidden
          .list-inner
            padding: 20px 30px
    .search-result
      position: fixed
      top: 124px
      bottom: 0
      width: 100%
    .tip-title
      text-align: center
      padding: 18px 0
      font-size: 0
      .icon-ok
        font-size: $font-size-medium
        color: $color-theme
        margin-right: 4px
      .text
        font-size: $font-size-medium
        color: $color-text
</style>
